package net.yxsoft.controller.pc.sysmgt.infrastructure;


import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import net.yxsoft.enumclass.StateEnumCls;
import net.yxsoft.enumclass.WhetherEnumCls;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.infrastructure.TsRole;
import net.yxsoft.service.infrastructure.MenuService;
import net.yxsoft.service.infrastructure.RoleService;
import rapid.jfinal.Pagination;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;


/**
* @MethodName: 角色管理Controller
* @Description:
* @param: null
* @return * @return null
* @throws
* @date 2023/8/31 15:30
* @author jhf
*/
public class RoleController extends AuthController {

  /**
   * Service
   */
  private static final RoleService roleService = RoleService.INSTANCE;
  private static final MenuService menuService = MenuService.INSTANCE;

  /**
  * @MethodName: index
  * @Description: 主视图
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:30
  * @author jhf
  */
  public void index() throws IllegalAccessException {
    List<WhetherEnumCls> whether = WhetherEnumCls.COLLECTS;
    List<StateEnumCls> state = StateEnumCls.defaults(StateEnumCls.class);

    setAttr("whether", whether);
    setAttr("state", state);

    render("role.html");
  }

  /**
  * @MethodName: openMenuPermission
  * @Description: 主视图
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:30
  * @author jhf
  */
  public void openMenuPermission() {
    setAttr("roleId", getPara("id"));
    render("_role_permission.html");
  }

  /**
  * @MethodName: getRoles
  * @Description: 获取角色列表
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:30
  * @author jhf
  */
  public void getRoles() {
    Kv cond = Kv.create();

    String name = getPara("name");
    String id = getPara("id");

    if(StrKit.notBlank(name)) {
      cond.set("r.NAME LIKE ", "%" + name + "%");
    }

    if(StrKit.notBlank(id)) {
      cond.set("r.ID = ", id);
    }

    renderJson(roleService.query("role.rolePageSelect", Kv.by("cond", cond), Pagination.by(this)));
  }



  /**
  * @MethodName: saveRole
  * @Description: 保存角色信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void saveRole() {

    renderJson(roleService.saveRole(getModel(TsRole.class, "", true), getLoginUser()));
  }

  /**
  * @MethodName: deleteRoleById
  * @Description: 根据主键删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void deleteRoleById() throws SQLException {
    renderJson(roleService.deleteRoleById(getPara("id")));
  }

  /**
  * @MethodName: deleteRoles
  * @Description: 根据主键集合删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void deleteRoles() throws SQLException {
    String[] ids = getPara("ids").split(",");
    renderJson(roleService.deleteRoles(ids));
  }

  /**
  * @MethodName: getAllRoleInfo
  * @Description: 获取全部角色信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void getAllRoleInfo() {
    renderJson(roleService.getAllRoleInfo());
  }

  /**
  * @MethodName: getMenusTree
  * @Description: 获取菜单树
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void getMenusTree() throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    renderJson(menuService.getMenusTree());
  }

  /**
  * @MethodName: checkMenus
  * @Description: 根据角色返回菜单权限
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:31
  * @author jhf
  */
  public void checkMenus() {
    //基本类型返回需用renderText
    renderText(roleService.checkMenus(getPara("roleId")));
  }

  /**
  * @MethodName: saveRolePermissions
  * @Description: 保存角色权限信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:32
  * @author jhf
  */
  public void saveRolePermissions() throws SQLException {
    String roleId = getPara("roleId");
    String menuIds = getPara("menuIds");
    renderJson(roleService.saveRolePermissions(roleId, StrKit.isBlank(menuIds) ? new String[0] : menuIds.split(",")));
  }

  /**
  * @MethodName: getWhether
  * @Description: 数据字典查询数据 动态表单全数据
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:32
  * @author jhf
  */
  public void getWhether(){
    renderJson(roleService.getWhether());
  }


}
